/*
import {
	getUserInfo
} from "../../api/user.js";*/
import cfg from '@/config/app';

import Cache from '@/utils/cache.js';

const state = {
	token: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsImlzcyI6InR4LnhtYi5qbC5jbiIsImF1ZCI6InR4LnhtYi5qbC5jbiIsImlhdCI6MTczNzQyODc0MiwibmJmIjoxNzM3NDI4NzQyLCJleHAiOjE3NDAwMjA3NDIsImp0aSI6eyJpZCI6MywidHlwZSI6ImFwaSJ9fQ.DE471wivYJ0eQ94VL51UfJCecSytjXXq9E6l6RBnF9w' || false,
	userInfo: Cache.get(cfg.USER_INFO) || {},
	uid: Cache.get(cfg.UID) || 0,
	config : {}
};

const mutations = {

	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(cfg.LOGIN_STATUS, opt.token,0);
		
	},
	
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	SET_CONFIG(state,config){
		let localConfig = Cache.get('config');
		if ( !localConfig ){
			Cache.set("config",config);
			localConfig = config;
		}
		state.config = localConfig;
	},
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		state.userInfo = {}
		Cache.clear(cfg.LOGIN_STATUS);
		Cache.clear(cfg.UID);
		Cache.clear(cfg.USER_INFO);
		Cache.clear('show_first');
	},
	
};

const actions = {
	USERINFO({
		state,
		commit
	}, force) {
		if (state.userInfo !== null && !force)
			return Promise.resolve(state.userInfo);
		else
			return new Promise(reslove => {
				getUserInfo().then(res => {
					commit("UPDATE_USERINFO", res.data);
					Cache.set(cfg.USER_INFO, res.data);
					reslove(res.data);
				});
			}).catch(() => {

			});
	}
};

export default {
	state,
	mutations,
	actions
};